import {createRouter, createWebHistory} from 'vue-router'
import Layout from "@/components/layout/Layout.vue";
import ConsultationList from "@/views/user/ConsultationList.vue";
import PsychologicalList from "@/views/psychological/PsychologicalList.vue";

const routes = [
    {
        path: '/',
        component: Layout,
        children: [
            {
                path: '/doctors',
                component: () => import('@/views/doctor/DoctorList.vue'),
                name: 'DoctorList',
                meta: {title: '医生列表', requireAuth: false}
            },
            {
                path: '/tests',
                component: () => import('@/views/test/AiTest.vue'),
                name: 'AiTest',
                meta: {title: '心理测试', requireAuth: false}
            },
            {
              path: '/forums',
              component: () => import('@/views/forum/ForumList.vue'),
              name: 'ForumList',
              meta: {title: '心理论坛', requireAuth: false}
            },
            {
              path: '/forum/:id',
              component: () => import('@/views/forum/ForumDetail.vue'),
              name: 'ForumDetail',
              meta: {title: '论坛详情', requireAuth: false}
            },
            {
                path: '',
                name: 'Home',
                component: () => import('../views/home/index.vue'),
                meta: {title: '首页'}
            },
            {
                path: '/psychoanalysis',
                component: PsychologicalList,
                name: 'PsychoanalysisList',
                meta: {title: '最新资讯', requireAuth: false}
            },
            {
                path: '/about',
                name: 'About',
                component: () => import('../views/about/About.vue'),
                meta: {title: '关于我们'}
            },
            {
                path: '/user',
                name: 'User',
                component: () => import('../views/user/UserCenter.vue'),
                meta: {title: '用户中心', requireAuth: true},
                children: [
                    {
                        path: '/user/publish',
                        name: 'Publish',
                        component: () => import('../views/user/PostPublish.vue'),
                    },
                    {
                        path: '/user/info',
                        name: 'Info',
                        component: () => import('../views/user/UserInfo.vue'),
                    },
                    {
                        path: '/user/appointments',
                        name: 'Appointments',
                        component: () => import('../views/user/AppointmentList.vue'),
                    },
                    {
                        path: '/user/consultation',
                        name: 'Consultation',
                        component: ConsultationList
                    }
                ]
            }
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/auth/Login.vue'),
        meta: {title: '登录'}
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('../views/auth/Register.vue'),
        meta: {title: '注册'}
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    // 设置页面标题
    document.title = to.meta.title ? `${to.meta.title} - 心理咨询管理系统` : '心理咨询管理系统'

    // 判断是否需要登录权限
    if (to.meta.requireAuth) {
        const user = localStorage.getItem('user')
        if (user) {
            next()
        } else {
            next({
                path: '/login',
                query: {redirect: to.fullPath}
            })
        }
    } else {
        next()
    }
})

export default router
